package qike.com.photodemo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private static final int TAKE_PHOTO = 1;
    private static final int CAMERA_REQUEST_CODE = 2;
    private static final int RESULT_REQUEST_CODE = 3;

    private static final String SAVE_AVATORNAME = "photo.png";
    private static final int IMAGE_REQUEST_CODE = 4;
    private Button btn_mr;
    private Button btn_mr1;
    private ImageView iv_mr;
    private Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        btn_mr = (Button) findViewById(R.id.btn_mr);
        btn_mr1 = (Button) findViewById(R.id.btn_mr1);
        iv_mr = (ImageView) findViewById(R.id.iv_mr);

        btn_mr.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //1.调起拍照
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(Environment.getExternalStorageDirectory(),SAVE_AVATORNAME)));
                startActivityForResult(intent,CAMERA_REQUEST_CODE);
            }
        });

        btn_mr1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //1.调起图库
                Intent intent = new Intent();
                intent.setType("image/*"); // 设置文件类型
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent, IMAGE_REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode!=RESULT_CANCELED){
            switch (requestCode){
                case CAMERA_REQUEST_CODE://获取保存的图片
                    startPhotoZoom(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), SAVE_AVATORNAME)));
                    break;
                case RESULT_REQUEST_CODE:
                    if(data!=null){
                        //保存裁剪之后的图片
                        Bundle extras = data.getExtras();
                        if (extras != null) {
                            Bitmap photo = extras.getParcelable("data");
                            saveMyBitmap(photo); // 保存裁剪后的图片到SD
                            iv_mr.setImageBitmap(photo);
                        }
                    }
                    break;
                case IMAGE_REQUEST_CODE:
                    startPhotoZoom(data.getData());
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    //保存裁剪之后的图片到SD卡
    private void saveMyBitmap(Bitmap bitmap) {
        File f = new File(Environment.getExternalStorageDirectory(), SAVE_AVATORNAME);
        try {
            f.createNewFile();
            FileOutputStream fOut = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);

            fOut.flush();
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void startPhotoZoom(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, RESULT_REQUEST_CODE);
    }
}
